package com.tgit.sso.paltform.util;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

/**
 * ClassName: SendTextMailUtil
 * date: 2019/11/25 19:31
 * Description: 邮件发送
 * @author fanzj@tgit.com
 * @since JDK 1.8
 */
public class SendTextMailUtil {




    //发件人地址
    public static String senderAddress = "1299578272@qq.com";
    //发件人账户名
    public static String senderAccount = "1299578272@qq.com";
    //发件人账户密码
    public static String senderPassword = "ulhoakarbkbpfdii";
    //会话
    private static volatile Session session;
    //属性
    public  static  Properties props;
    static{
        props = new Properties();
        //设置用户的认证方式
        props.setProperty("mail.smtp.auth", "true");
        //设置传输协议
        props.setProperty("mail.transport.protocol", "smtp");
        //设置发件人的SMTP服务器地址
        props.setProperty("mail.smtp.host", "smtp.qq.com");
        props.setProperty("mail.smtp.port", "587");
        //在初始化邮件的时候加入这些参数，设置下超时时间，默认的是无穷大
        props.put("mail.smtp.timeout", 30000);
        props.put("mail.smtp.connectiontimeout", 30000);
        props.put("mail.smtp.writetimeout", 30000);
        //2、创建定义整个应用程序所需的环境信息的 Session 对象
        session = Session.getInstance(props);
        session.setDebug(true);
    }

    public static synchronized void sendEmail(String receive,String validCode) throws Exception {
        //3、创建邮件的实例对象
        Message msg = getMimeMessage(session,receive,validCode);
        //4、根据session对象获取邮件传输对象Transport
        Transport transport = session.getTransport();
        //设置发件人的账户名和密码
        transport.connect(senderAccount, senderPassword);
        //发送邮件，并发送到所有收件人地址，message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
        transport.sendMessage(msg, msg.getAllRecipients());
        //如果只想发送给指定的人，可以如下写法
        //transport.sendMessage(msg, new Address[]{new InternetAddress("xxx@qq.com")});
        //5、关闭邮件连接
        transport.close();
    }

    /**
     * 获得创建一封邮件的实例对象
     *
     * @param session
     * @return
     * @throws Exception
     * @throws Exception
     */
    public static MimeMessage getMimeMessage(Session session,String receive,String validCode) throws Exception {
        //创建一封邮件的实例对象
        MimeMessage msg = new MimeMessage(session);
        //设置发件人地址
        msg.setFrom(new InternetAddress(senderAddress));
        /**
         * 设置收件人地址（可以增加多个收件人，发送、抄送、密送），即下面这一行代码书写多行
         * MimeMessage.RecipientType.TO:发送
         * MimeMessage.RecipientType.CC：抄送
         * MimeMessage.RecipientType.BCC：密送
         */
        //
        Address[] internetAddressTo = new InternetAddress().parse(receive);
        //msg.setRecipient(MimeMessage.RecipientType.BCC, new InternetAddress(recipientAddress));
        msg.setRecipients(MimeMessage.RecipientType.TO,internetAddressTo);
        //设置邮件主题
        msg.setSubject("*********企业用户信息签名认证**************", "UTF-8");
        //设置邮件正文
        msg.setContent("欢迎使用***平台****  \n <br>尊敬的"+receive+":<br>   您好!  您的注册(修改密码)授权信息:"+validCode, "text/html;charset=UTF-8");
        //设置邮件的发送时间,默认立即发送
        msg.setSentDate(new Date());

        return msg;
    }

}
